package com.vettukal.sync;

import java.util.ArrayList;
import java.util.List;

import com.google.web.bindery.requestfactory.shared.Receiver;
import com.vettukal.sync.shared.TaskProxy;
import com.vettukal.sync.shared.TestMsgRequestFactory;

import android.content.ReceiverCallNotAllowedException;
import android.os.AsyncTask;
import android.util.Log;



public class AsyncTaskList extends AsyncTask<Long, Void, String[] > {
	
	TestMsgActivity activity;
	static String tag = "vincent";
	
	public AsyncTaskList(TestMsgActivity activity) {
		this.activity = activity;
	}

	@Override
	protected String[] doInBackground(Long... params) {
		
		Log.i("vincent", "inside the async");
		
		final List<TaskProxy> list = new ArrayList<TaskProxy>();
		TestMsgRequestFactory factory = Util.getRequestFactory(activity, TestMsgRequestFactory.class);
		factory.taskRequest().queryTasks().fire(new Receiver<List<TaskProxy>>() {

			@Override
			public void onSuccess(List<TaskProxy> arg0) {
				Log.i("vincent", "inside succes");
				
				for(TaskProxy taskpro : arg0) {
					Log.d(tag,"name:"+taskpro.getName());
					Log.d(tag, "note:"+taskpro.getNote());
					Log.d(tag, "user:"+taskpro.getEmailAddress());
				}
				Log.d(tag, "Size of list"+list.size());
				list.addAll(arg0);
				Log.d(tag, "Size of list"+list.size());
				
			} 
			
			
		});
		
		Log.i("vincent", "async completed taking the list");
		int il = list.size();
		String[] notelist = new String[il];
		notelist[0]= "vincent 0";
		int i=0;
		for(TaskProxy task: list) {
			notelist[i]=task.getName();
			
			Log.d(tag,"list name:"+task.getName());
			Log.d(tag, "list note:"+task.getNote());
			Log.d(tag, "list user:"+task.getEmailAddress());
			Log.i("vincent", "inside the string"+notelist[i]);
			
			i++;
		}
		
		return notelist;
	}
	
	protected void onPostExecute(String[] notelist) {
		activity.updateListView(notelist);
	}
	
}
